<?php
namespace Common\Mysql;
class SiteTagsMysql extends CommonMysql {
    public function addSiteTag($title,$sid) {
        verify($title,'notEmpty','标签名称为空');

        verify($sid,'notEmpty','网站ID不能为空');

        $tagInfo = D('Tags','Mysql')->getByTitle($title);
        if($tagInfo) {
            $where = array(
                'sid'   =>  $sid,
                'tagid' =>  $tagInfo['id']
            );
            if($this->where($where)->count() > 0) {
                E('分类 "' .$title. '" 已经存在');
            }
            $tagid = $tagInfo['id'];
        }else {
            $tagid = D('Tags','Mysql')->addTag($title);
        }

        $id = $this->addRecord(array(
            'sid'       =>  $sid,
            'tagid'     =>  $tagid,
            'hotnum'    =>  0,
            'createtime'=>  NOW_TIME,
            'updatetime'=>  NOW_TIME,
            'status'    =>  1,
        ),false,'网站标签添加失败');

        return $id;
    }

    public function addTags($tags,$sid) {
        verify($sid,'notEmpty','网站ID不能为空');

        foreach($tags as $title) {

            if(!$title) continue;

            $tagInfo = D('Tags','Mysql')->getByTitle($title);
            if(!$tagInfo) {
                $tagid = D('Tags','Mysql')->addTag($title);
            }else {
                $tagid = $tagInfo['id'];
                if($this->where(array('sid'=>$sid,'tagid'=>$tagInfo['id']))->count() > 0) {
                    continue;
                }
            }

            $this->addRecord(array(
                'sid'       =>  $sid,
                'tagid'     =>  $tagid,
                'hotnum'    =>  0,
                'createtime'=>  NOW_TIME,
                'updatetime'=>  NOW_TIME,
                'status'    =>  1,
            ));
        }
    }
}
?>

